var express = require('express');
var router = express.Router();

var menudao = require('../dao/menudao');

if (process.env.NODE_ENV || "development" === "development") {
  /*
  var knex = require('../dbhelper').instance();
  //数据库连接测试
  knex('sys_config')
    .select('*')
    .then(function (result) {
      console.log(result);
    }).catch(function (err) {
      console.error(err);
    });

  var utils = require('../common/utils');
  console.log(utils.generalShortUrl('jacklklkl'));
  console.log(utils.generalUUID());
*/
}

/* GET home page. */
router.get('/', function (req, res, next) {
  var userid = req.session.user.userid;
  menudao.getmenu(userid).then(function (data) {
    var item = [];
    data.forEach(element => {
      item.push(element.parentname);
    });
    res.render('index', {
      menu: data,
      items: unique(item),
      user:req.session.user
    });
  }, function (error) {
    res.render('500');
  })
});

router.get('/welcome', function (req, res, next) {
  res.render('welcome', {
    title: 'Express'
  });
});

function unique(arr) {
  var ret = []
  for (var i = 0; i < arr.length; i++) {
    var item = arr[i]
    if (ret.indexOf(item) === -1) {
      ret.push(item)
    }
  }
  return ret
}


module.exports = router;